package com.bjtg.threegoclinet.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.TextUtils;

import com.bjfl.saleManager.util.BaiduTTSSpeakUtil;
import com.bjtg.threegoclinet.bean.GlobleMessage;
import com.bjtg.threegoclinet.base.MyApplication;
import com.bjtg.threegoclinet.bean.Constants;
import com.bjtg.threegoclinet.okhttpUtils.HttpConstants;
import com.bjtg.threegoclinet.okhttpUtils.OkHttpClientManager;
import com.bjtg.threegoclinet.util.IsInternet;
import com.log.LatteLogger;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

import static com.bjtg.threegoclinet.util.JpushSharedPreferences.parameterSet;

public class HeartBeatService extends Service {
    private String rid;

    public HeartBeatService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (TextUtils.isEmpty(rid)) {
            rid = JPushInterface.getRegistrationID(MyApplication.getApplicationMyContext());
            BaiduTTSSpeakUtil.getInstance().speak(IsInternet.NETWORK_NONE_TEXT);
        } else {
            LatteLogger.d("rid", rid);
            postHeartbean(rid);
        }
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int tenSencond = 30 * 1000;
        long triggerAtTime = SystemClock.elapsedRealtime() + tenSencond;
        Intent i = new Intent(this, HeartBeatService.class);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        manager.cancel(pi);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return super.onStartCommand(intent, flags, startId);
    }

    private void postHeartbean(String rid) {
        // 心跳接口
        HashMap<String, String> map = new HashMap<>();
        map.put("apiKey", "408c71b7dc404e00a9018035bb7855a1");
        map.put("version", "1.0");
        map.put("machineNo", parameterSet.getMachineNo());//与消息别名匹配
        LatteLogger.d("当前机器编号:", parameterSet.getMachineNo());

        if (rid == null || "".equals(rid)) {
            rid = JPushInterface.getRegistrationID(MyApplication.getApplicationMyContext());
            //设置rid
            GlobleMessage.getParamSet().setRid(rid);
            parameterSet.setRid(rid);
        }

        map.put("registrationId", rid);
        //读取货道当前状态(状态值根据货道协议定义)
        map.put("status", parameterSet.getMachineStatus());
        postHeart(map);
    }

    private long heartTimes;

    private void postHeart(HashMap<String, String> map) {
        OkHttpClientManager.getInstance().postAsync(Constants.getHeartbeat(), HttpConstants.heartbeat,
                map, new OkHttpClientManager.HttpCallback() {
                    @Override
                    public void onSuccess(String result, int code) {
                        if (heartTimes == 1) {
                            BaiduTTSSpeakUtil.getInstance().speak("欢迎您使用ThreeGo");
                        }
                        heartTimes++;
                        LatteLogger.d("result", result);
                    }

                    @Override
                    public void onStart(int code) {

                    }

                    @Override
                    public void onFailure(int code) {

                        BaiduTTSSpeakUtil.getInstance().speak(IsInternet.NETWORK_NONE_TEXT);
                        LatteLogger.d("result", "服务异常！");
                        heartTimes = 0;
                        postHeart(map);
                    }
                });
    }
}
